package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  11:31 2020/4/9</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/4/9      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Service
public class PageServiceImpl implements PageService {

    @Value("${pagepath}")
    private String pagepath;

    private Logger log = LoggerFactory.getLogger(PageServiceImpl.class);
    @Autowired
    private TemplateEngine templateEngine; //模板引擎
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SkuFeign skuFeign;

    /**
     * @Date: 11:31 2020/4/9
     * @Param: [spuId]
     * @return: void
     * @Description: 根据spuId生成静态页
     **/
    @Override
    public void createHtml(String spuId) {
        //1.页面填充内容
        Context context = new Context();
        context.setVariables(buildPageMap(spuId));
        //2.输出流
        Writer writer = null;
        File file = new File(pagepath);
        try {
            writer = new PrintWriter(file + "/" + spuId + ".html");
            templateEngine.process("item", context, writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(null != writer){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
    * @Date: 14:18 2020/4/9
    * @Param: [spuId]
    * @return: java.util.Map<java.lang.String,java.lang.Object>
    * @Description: 构建页面数据
    **/
    private Map<String,Object> buildPageMap(String spuId) {
        Map<String,Object> resultMap = new HashMap<>();
        //1.spu
        Spu spu = spuFeign.findById(spuId);
        if(null != spu){
            resultMap.put("spu", spu);
            //2.skuList
            List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
            resultMap.put("skuList", skuList);
            //3.specificationList map 规格
            String specItems = spu.getSpecItems();
            Map map = JSON.parseObject(specItems, Map.class);
            resultMap.put("specificationList", map);
            //4.分类名称---三级都需要
            resultMap.put("category1", categoryFeign.findById(spu.getCategory1Id()));
            resultMap.put("category2", categoryFeign.findById(spu.getCategory2Id()));
            resultMap.put("category3", categoryFeign.findById(spu.getCategory3Id()));
            //5.图片list--->imageList
            String[] imageList = spu.getImages().split(",");
            resultMap.put("imageList", imageList);

        }
        return resultMap;
    }

}
